#include <bitset>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <istream>
#include <queue>
#include <utility>
#include <vector>
using ll = int64_t;
#define sl static inline
#define printf
const ll m=10;
ll n,k;
std::vector<std::vector<char>> v;
std::vector<std::bitset<m+1>> vis;
const ll dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
std::vector<std::pair<ll, ll>> mb;

sl bool find(ll i,ll j){
    printf("--START i=%lld, j=%lld\n",i,j);
    mb.clear();
    ll num=1;
    std::queue<std::pair<ll, ll>> q;
    q.emplace(i,j);
    vis[i][j]=true;
    char c=v[i][j];
    while(q.size()){
        auto[x,y]=q.front();
        q.pop();
        for(ll k=0;k<4;k++){
            ll nx=x+dir[k][0],ny=y+dir[k][1];
            if(nx<1||nx>n||ny<1||ny>m||c!=v[nx][ny]||vis[nx][ny])continue;
            vis[nx][ny]=true;
            num++;
            q.emplace(nx,ny);
        }
        if(num>=k){
            v[x][y]='0';
            printf("find x=%lld, y=%lld\n",x,y);
        }else{
            mb.emplace_back(x,y);
        }
    }
    if(num>=k){
        for(auto[x,y]:mb){
            v[x][y]='0';
            printf("find x=%lld, y=%lld\n",x,y);
        }
        mb.clear();
    }
    return num>=k;
}

sl void pm(){
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            printf("%c",v[i][j]);
        }
        printf("\n");
    }
    printf("---\n");
}

sl void move(){
    for(ll j=1;j<=m;j++){
        ll l=-1;
        for(ll i=n;i>=1;i--){
            if(v[i][j]=='0'&&l==-1)l=i;
            else if(l!=-1&&v[i][j]!='0'){
                v[l][j]=v[i][j];
                v[i][j]='0';
                l--;
            }
        }
    }
}

sl bool check(){
    vis.clear();
    vis.resize(n+1);
    bool ret=false;
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            if(v[i][j]!='0'){
                if(find(i,j)){
                    pm();
                    pm();
                    ret=true;
                }
            }
        }
    }
    move();
    return ret;
}

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n>>k;
    v.resize(n+1,std::vector<char>(m+1));
    vis.resize(n+1);
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            std::cin>>v[i][j];
        }
    }
    while (check());
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            std::cout<<v[i][j];
        }
        std::cout<<"\n";
    }
}